<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: andery
 * Date: 13-12-5
 * Time: 下午9:02
 */

class MY_Input extends CI_Input {

    //获取参数静态变量
    private static $urls = null;

    /**
     * post处理
     * @author duanlang<duanlang@iyene.com>
     * @param  [type] $index [description]
     * @param  boolean $xss_clean [description]
     * @return [type] [description]
     */
    function post($index = NULL, $xss_clean = FALSE)
    {
        // 兼容application/json
        if (isset($_SERVER['CONTENT_TYPE']) && strstr($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
            $data = json_decode(file_get_contents("php://input"), true);
        } else {
            $data = $_POST;
        }
        // Check if a field has been provided
        if ($index === NULL AND ! empty($data))
        {
            $post = array();

            // Loop through the full _POST array and return it
            foreach (array_keys($data) as $key)
            {
                $post[$key] = $this->_fetch_from_array($data, $key, $xss_clean);
            }
            return $post;
        }

        return $this->_fetch_from_array($data, $index, $xss_clean);
    }
    /**
     * 获取URL地址参数数组
     *
     * @return array|null
     */
    public function urls(){
        if(is_array(self::$urls)) return self::$urls;
        $CI = &get_instance();
        $urls = $CI->uri->ruri_to_assoc();
        if(!is_array($urls)) $urls = array();
        self::$urls = $urls;
        return self::$urls;
    }
     /**
     * 将URL地址参数数组合并到GET和REQUEST中
     */
    public function set_urls(){
        if(!is_array(self::$urls)){
            $this->urls();
            $_GET = array_merge(self::$urls,$_GET);
            $_REQUEST = array_merge(self::$urls,$_REQUEST);
        }
    }
    /**
     * 获取所有请求数据
     *
     * @param bool $xss_clean 是否清除XSS威胁
     * @return array 所有请求数据
    */
    public function requests($xss_clean = FALSE)
    {
        if(!is_array($_REQUEST)) return array();
        $this->set_urls();
        $result = (object)array();
        foreach(array_keys($_REQUEST) as $v)
        {
            $result->$v = $this->get_post($v, $xss_clean);
        }
        return $result;
    }
}